package bean.util;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;

import bean.control.ProductControl;

import amazon.s3.S3;
import eao.EaoProductPicture;
import entity.Product;
import entity.ProductPicture;
import entity.UserPicture;

import util.Crypto;
import util.ImageUtil;
import util.UploadedImage;
import util.Util;

/**
* @author Ilya Shaikovsky
* 
*/
@ManagedBean(name="product_file_upload")
@ViewScoped
public class ProductFileUpload implements Serializable {
	private static final long serialVersionUID = -8885211174733409053L;
	
	@ManagedProperty("#{productc}")
	ProductControl productc;
	
	private ArrayList<UploadedImage> files = new ArrayList<UploadedImage>();
	private String path ="C:\\SuaParte\\tmp\\";
   
   public void paint(OutputStream stream, Object object) throws IOException {
       stream.write(getFiles().get((Integer) object).getData());
       stream.close();
   }

   public void listener(FileUploadEvent event) throws Exception {
       UploadedFile item = event.getUploadedFile();
       UploadedImage ui = new UploadedImage();
       
       String name = Util.getCurrentTime() + item.getName().substring(item.getName().length() - 4, item.getName().length());
       
       File file = new File(path + name);
       FileOutputStream fos = new FileOutputStream(file);
       fos.write(item.getData());
       
       ui.setLength(item.getData().length);
       ui.setName(name);
       ui.setData(item.getData());
       ui.setFile(file);
       
       files.add(ui);
       
       String code = Crypto.encrypt(productc.getUserb().getUser().getEmail());
       this.uploadPicture(code);
   }
   	
	public void uploadPicture(String code) {
		S3 s3 = new S3();
		for (UploadedImage ui :files){
			s3.upload("/user/" + productc.getUserb().getUser().getEmail() +"/products/" + code , ui.getName(), ui.getFile());
			productc.getProductb().getPictures().add("https://s3.amazonaws.com/SuaParte/user/" + productc.getUserb().getUser().getEmail() + "/products/"+ code + "/" + ui.getName());
		}
		
		files.clear();
	}
   
   public String clearUploadData() {
       files.clear();
       return null;
   }

   public int getSize() {
       if (getFiles().size() > 0) {
           return getFiles().size();
       } else {
           return 0;
       }
   }

   public long getTimeStamp() {
       return System.currentTimeMillis();
   }

   public ArrayList<UploadedImage> getFiles() {
       return files;
   }

   public void setFiles(ArrayList<UploadedImage> files) {

       this.files = files;
   }

public ProductControl getProductc() {
	return productc;
}

public void setProductc(ProductControl productc) {
	this.productc = productc;
}
   
   

}